home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MacWorld: Complete Mac Interactive
/
Macworld Complete Mac Interactive CD)(1994).iso
/
The Best of BMUG
/
Utilities
/
Text and Speech
/
Alpha.5.76
/
Tcl
/
SystemCode
/
fill.tcl
< prev
next >
Wrap
Text File
|
1994-03-16
|
4KB
|
184 lines
#=============================================================================
# 'Fill' routines.
#=============================================================================
proc fillParagraph {} {
set pos [getPos]
set start [paraStart $pos]
set finish [paraFinish $pos]
goto $start
set text [fillText $start $finish]
replaceText $start $finish $text "\r"
goto $pos
}
proc fillParagraphSentence {} {
set pos [getPos]
set start [paraStart $pos]
set finish [paraFinish $pos]
regsub -all "\[ \t\r\]+" [getText $start $finish] " " text
regsub -all {\. } $text "Δ" text
set result ""
foreach line [split $text "Δ"] {
if {[string length $line]} {
append result [breakIntoLines $line] ".\r"
}
}
replaceText $start $finish $result
goto $pos
}
proc getEndpts {} {
if {[getPos] == [selEnd]} {
set start [getPos]
set finish [getMark]
if {$start > $finish} {
set temp $start
set start $finish
set finish $temp
}
} else {
set start [getPos]
set finish [selEnd]
}
return [list $start $finish]
}
proc fillRegion {} {
set ends [getEndpts]
set start [lineStart [lindex $ends 0]]
set finish [lindex $ends 1]
goto $start
set text [fillText $start $finish]
replaceText $start $finish $text "\r"
}
proc wrapParagraph {} {
set pos [getPos]
set start [paraStart $pos]
set finish [paraFinish $pos]
goto $start
wrapText $start $finish
goto $pos
}
proc wrapRegion {} {
set ends [getEndpts]
set start [lineStart [lindex $ends 0]]
set finish [lindex $ends 1]
if {$start == $finish} {
set finish [maxPos]
}
wrapText $start $finish
}
proc paraStart {pos} {
if {$pos == [maxPos]} {incr pos -1}
set res [search -n -f 0 -r 1 -l 0 {^([ \t]*|(%.*))$} $pos]
if {![string length $res]} {return 0}
return [nextLineStart [lindex $res 0]]
}
proc paraFinish {pos} {
set end [maxPos]
set res [search -n -f 1 -r 1 -l $end {^([ \t]*|(%.*))$} $pos]
if {![string length $res]} {return $end}
return [lindex $res 0]
}
proc oldParaStart {pos} {
set pos [lineStart $pos]
while {$pos > 0} {
set back [lineStart [expr $pos-1]]
if {$back < 0} {return 0}
if {[regexp "^\[ \t\]*\r$" [getText $back $pos]]} {return $pos}
set pos $back
}
return 0
}
proc oldParaFinish {pos} {
set end [maxPos]
while {$pos < $end} {
set next [nextLineStart $pos]
if {$pos == "-1"} {return $end}
if {[regexp "^\[ \t\]*\r$" [getText $pos $next]]} {return $pos}
set pos $next
}
return $end
}
# Remove text from window, transform, and insert back into window.
# Alternate version of fillText. Probably better but slower.
# proc fillText {from to} {
# set text [getText $from $to]
# regsub -all "\[ \t\r\]+" $text " " text
# regsub -all {\. } $text {. } text
# set text [string trimright [breakIntoLines $text]]
# set a "a"
# set a [breakIntoLines $a]
# regsub "a" $a "" a
# set a \r$a
# set b $a
# regsub -all [append b " "] $text $a text
# return $text
# }
proc fillText {from to} {
set text [getText $from $to]
regsub -all "\[ \t\r\]+" $text " " text
regsub -all {\. } $text {. } text
return [string trim [breakIntoLines $text]]
}
proc lineToParagraph {} {
global fillColumn
global leftFillColumn
saveVars
set fillColumn 10000
set leftFillColumn 0
fillRegion
restoreVars
}
proc paragraphToLine {} {
global fillColumn
global leftFillColumn
saveVars
set fillColumn 75
set leftFillColumn 0
fillRegion
restoreVars
}
#set sentEnd {[.!?](\r| +)}
set sentEnd {(\r\r|[.!?](\r| +))}
set sentBeg {[\r ][A-Z]}
proc nextSentence {} {
global sentBeg sentEnd
if {![catch {search -f 1 -r 1 $sentEnd [getPos]} mtch]} {
if {![catch {search -f 1 -r 1 -i 0 $sentBeg [expr [lindex $mtch 1]-1]} mtch]} {
goto [expr [lindex $mtch 0]+1]
}
}
}
proc prevSentence {} {
global sentBeg sentEnd
if {[catch {search -f 0 -r 1 $sentBeg [expr [getPos]-2]} mtch]} return
if {![catch {search -f 0 -r 1 $sentEnd [lindex $mtch 1]} mtch]} {
if {![catch {search -f 1 -r 1 -i 0 $sentBeg [expr [lindex $mtch 1]-1]} mtch]} {
goto [expr [lindex $mtch 0]+1]
}
}
}